<template>
  <div id="news" class="main">
    <div class="contaier" v-loading="loading">
      <div class="left-new">
        <div class="df">
          <div class="f14" style="margin-right: 10px">
            共 <span style="color: var(--color-text)">{{ total }}</span> 条
          </div>
          <div class="search-box">
            <el-popover
              placement="bottom-start"
              trigger="click"
              :visible="popperVis"
              popper-class="popoverStyle"
            >
              <template #reference>
                <el-input
                  @focus="handleInputFocus"
                  @blur="handleInputBlur"
                  size="large"
                  clearable
                  :prefix-icon="Search"
                  v-model="searchvalue"
                  style="width: 100%"
                  placeholder="搜索"
                >
                  <template #append>
                    <!-- style="background: var(--background-color); color: #fff" -->
                    <el-button size="large" @click="handleTbaleSearch"
                      >搜索</el-button
                    >
                    <!-- <div class="my-btn">搜索</div> -->
                  </template>
                </el-input>
              </template>
              <div style="color: #999; font-size: 12px">历史记录</div>
              <div class="curr">
                <div
                  class="his-item"
                  v-for="item in searchHistory"
                  :key="item"
                  @click="handleClickHis(item)"
                >
                  {{ item }}
                </div>
              </div>
            </el-popover>
          </div>
        </div>
        <div v-infinite-scroll="load" :infinite-scroll-immediate="false">
          <div v-for="item in newList" :key="item.id">
            <div class="left">
              <div class="imgs" v-if="item.image_url">
                <img :src="item.image_url" alt="" />
              </div>
              <div style="flex: 1">
                <h3 class="title curr" @click="handleDetTitle(item)">
                  {{ item.title }}
                </h3>
                <div style="color: #666" class="f14">
                  <div style="margin: 20px 0">{{ item.source }}</div>
                  <span>{{ item.update_time }}</span>
                  <span style="margin: 0 10px">|</span>
                  <span>{{ item.view_total }}次浏览</span>
                </div>
              </div>
              <!-- <div></div> -->
            </div>
          </div>
        </div>
        <div
          v-if="newList.length <= 0"
          style="
            width: 850px;
            text-align: center;
            margin-top: 100px;
            color: #666;
          "
          class="f14"
        >
          暂无数据
        </div>
      </div>
      <div style="width: 300px; margin: 0px 0 0 40px">
        <hotList :hostDataList="hostDataList" url="/news/detail" type="1" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { getHotNews } from '@/api/news/news';
import hotList from '@/components/HotList';
import imgUrl from '@/assets/images/news-hot-banner.f2085d4.png';
import { Search } from '@element-plus/icons-vue';

import { ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
const total = ref(0);
const loading = ref(false);
const popperVis = ref(false);
const searchvalue = ref('');
const searchHistory = ref([]);
const hostDataList = ref({
  title: '热门资讯',
  hotList: [],
  imgUrl: imgUrl,
});
const newList = ref([]);
let reqNewData = ref({
  page: 1,
  pageSize: 10,
});
const load = () => {
  reqNewData.value.page += 1;
  fetchHotNews();
};
const handleDetTitle = (item) => {
  router.push({
    path: '/news/detail',
    query: {
      id: item.id,
    },
  });
};
const handleTbaleSearch = () => {
  reqNewData.value.page = 1;
  reqNewData.value.search = searchvalue.value;
  if (searchvalue.value) {
    searchHistory.value.push(searchvalue.value);
  }
  newList.value = [];
  fetchHotNews();
};
const handleInputFocus = () => {
  if (searchHistory.value.length <= 0) {
    popperVis.value = false;
  } else {
    popperVis.value = true;
  }
};
const handleInputBlur = () => {
  popperVis.value = false;
};
const handleClickHis = (item) => {
  reqNewData.value.search = item;
  newList.value = [];
  fetchHotNews();
};
const fetchHotNews = () => {
  loading.value = true;
  getHotNews(reqNewData.value)
    .then((res) => {
      if (res.data.code == 20000) {
        loading.value = false;
        total.value = res.data.total;
        hostDataList.value.hotList = res.data.data;
        newList.value = [...newList.value, ...res.data.data];
      } else {
        loading.value = false;
      }
    })
    .catch(() => {
      loading.value = false;
    });
};
fetchHotNews();
</script>

<style lang="scss" scoped>
#news {
  .contaier {
    display: flex;
    margin-top: 10px;
  }
  .imgs {
    width: 220px;
    height: 150px;
    margin-right: 20px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .left {
    display: flex;
    margin: 20px 0;
  }
  .title {
    &:hover {
      color: var(--color-text);
    }
  }
}
.search-box {
  flex: 1;
  background: #fff;
}
:deep(.el-input-group__append) {
  width: 70px;
  background: var(--background-color);
  color: #fff;
}
.his-item {
  padding: 5px 0 5px 0px;
  // margin: 10px 0;
  &:hover {
    background: #f3f6f9;
  }
}
</style>
